/*
 * @Description: 泛型接口
 * @Autor: HuiSir<273250950@qq.com>
 * @Date: 2021-05-06 18:35:53
 * @LastEditTime: 2021-05-20 15:10:33
 */
var User = /** @class */ (function () {
    function User(name, age) {
        this.name = name;
        this.age = age;
    }
    return User;
}());
/**
 * @description: 用户创建和查询类
 * @param {*}
 * @return {*}
 * @author: HuiSir
 */
var UserApi = /** @class */ (function () {
    function UserApi() {
        this.data = []; //初始化为空数组
    }
    UserApi.prototype.saveUser = function (user) {
        // 创建不重复id
        user.id = Date.now() + Math.floor(Math.random() * 1000);
        this.data.push(user);
        return user;
    };
    // 由于查询可能查不到，所以可能返回为undefined
    UserApi.prototype.getUserById = function (id) {
        return this.data.find(function (user) { return user.id === id; });
    };
    return UserApi;
}());
var userApi = new UserApi();
// 没有数据的情况
console.log(userApi.getUserById(1)); //undefined
// 添加用户
userApi.saveUser(new User("张三", 11));
var id = userApi.saveUser(new User("李四", 13)).id;
userApi.saveUser(new User("王五", 15));
// 查看数据
console.log(userApi.data, id);
// [
//     User { name: '张三', age: 11, id: 1621259466267 },
//     User { name: '李四', age: 13, id: 1621259466455 },
//     User { name: '王五', age: 15, id: 1621259466528 }
// ]
// id: 1621259466455
// 查询用户ById
// 这里在严格模式下id可能为undefined,而getUserById的参数时number,所以要加！断言
// 在非严格模式下不会无须断言
console.log(userApi.getUserById(id)); //User { name: '李四', age: 13, id: 1621259618344 }
